package com.zj.leetcode.easy.listNode;

/**
 * @program: algorithm
 * @description: 反转链表
 * 剑指 Offer 24 fan-zhuan-lian-biao-lcof
 * @author: Zhang Bo
 * @create: 2022-07-28 10:22:49
 **/
public class FanZhuanLianBiaoLcof {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
        Solution solution = new FanZhuanLianBiaoLcof().new Solution();
        System.out.println(solution.reverseList(listNode));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
        ListNode(int x, ListNode next) {
            this.next = next;
            val = x;
        }
    }

    class Solution {
        /**
         * 1->2->3->4->5
         * @param head
         * @return
         */
        public ListNode reverseList(ListNode head) {

            ListNode node = null;
            while (head != null) {
                ListNode temp = head;
                head = head.next;
                temp.next = node;
                node = temp;
            }
            return node;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
